#include "stdafx.h"

char cCustomRenameAll[33];
char cCustomRenameClient[33];
bool bWasDead = false;
bool bShouldSetAntiQuit = false;

void RME_Thread() {
	// AUTH CHECK FIRST
	DbgPrint("Init Thread!");
	*(int*)0x825c5330 = 0x60000000; //test
	*(int*)0x822781D8 = 0x60000000; //cipher premium kick patch
	*(int*)0x82273C88 = 0x60000000; //cipher offhost freeze patch  
	*(int*)0x822781D8 = 0x60000000; //cipher offhost kick patch
	*(int*)0x82717D48 = 0x60000000;
	/// Disable stupid assertions on devkit
//	*(int*)0x8177c120 = 0x60000000;
	//*(int*)0x8177cbf0 = 0x60000000;





	// Apply our autowall fix patch
	*(DWORD*)0x82259b40 = 0x60000000;
	*(DWORD*)0x82497EB0 = 0x60000000;
	*(DWORD*)0x82497F30 = 0x60000000;
	*(DWORD*)0x82497EE0 = 0x60000000;
	*(DWORD*)0x82497EC8 = 0x60000000;
	*(DWORD*)0x825996AC = 0x60000000;
	*(DWORD*)0x8259A65C = 0x60000000; //should be all needed ones, please correct me if im wrong, i dont need this just yet, delete if you dont wanna do console commands

	*(DWORD*)0x82003f70 = 0x00000000; //for showing host, doing 
	*(int*)0x826A5FBC = 0x60000000; //infinite class items (0x7F43D214)
	//*(DWORD*)0x82003f70 = 0x00000000; //for showing host, doing it just as XBOX360LSBEST did in his shittool
	//*(BYTE*)0x83C5A3BB = 0x01; //this sets the show host shit to true

	while (!CG::bKillThread) {

		if (MenuOptions::bRME_GodMode) {
			if (CheckInGame() && CG::bLobbyInitialized) {
				if (!IsAlive(get_cg()->clientNumber)) {
					if (!bWasDead) {
						bWasDead = true;
					}
				}
				else {
					if (bWasDead) {
						Sleep(500);

						ChangeGodMode(-1, true);
						bWasDead = false;
					}
				}
			}
			else {
				if (!bWasDead) {
					bWasDead = true;
				}
			}
		}

		if (MenuOptions::bAntiQuitAuto) {
			if (CheckInGame() && CG::bLobbyInitialized) {
				if (bShouldSetAntiQuit) {
					Sleep(500);

					AntiQuit(true);

					bShouldSetAntiQuit = false;
				}
			}
			else {
				if (!bShouldSetAntiQuit) {
					bShouldSetAntiQuit = true;
				}
			}
		}

		if (MenuOptions::bMakeEveryoneThrowNades) {
			ThrowGrenadesForEveryone();
		}

		if (CheckInGame() && CG::bLobbyInitialized) {
			if (MenuOptions::bCustomRenameAllClients) {
				MenuOptions::bCustomRenameAllClients = FALSE;

				wchar_t wcOldGT[33];
				wchar_t wcGT[33];
				XOVERLAPPED XOver;
				ZeroMemory(&XOver, sizeof(XOVERLAPPED));
				XShowKeyboardUI(0, VKBD_HIGHLIGHT_TEXT, wcOldGT, L"Set Gamertag", L"Please enter a gamertag you'd like to set for all clients.", wcGT, 33, &XOver);

				while (!XHasOverlappedIoCompleted(&XOver)) Sleep(25);

				Sleep(100);

				if (wcGT[0] != 0 && wcGT[1] != 0)
				{
					wcscpy_s(wcOldGT, wcGT);
					wcstombs(cCustomRenameAll, wcGT, 33);

					RenameAll(cCustomRenameAll);
				}
			}

			if (MenuOptions::bRenameSelectedClient) {
				MenuOptions::bRenameSelectedClient = FALSE;

				wchar_t wcOldGT[33];
				wchar_t wcGT[33];
				XOVERLAPPED XOver;
				ZeroMemory(&XOver, sizeof(XOVERLAPPED));
				XShowKeyboardUI(0, VKBD_HIGHLIGHT_TEXT, wcOldGT, L"Set Gamertag", L"Please enter a gamertag you'd like to set this player.", wcGT, 33, &XOver);

				while (!XHasOverlappedIoCompleted(&XOver)) Sleep(25);

				Sleep(100);

				if (wcGT[0] != 0 && wcGT[1] != 0)
				{
					wcscpy_s(wcOldGT, wcGT);
					wcstombs(cCustomRenameClient, wcGT, 33);

					RenameSelectedClient(SelectedPlayer, cCustomRenameClient);
				}
			}

			if (gAutoBlacklist.size() > 0) {
				for (int i = 0; i < gAutoBlacklist.size(); i++) {
					if (gAutoBlacklist[i].XUID != NULL) {
						int clientNum = GetClientNumFromXUID(gAutoBlacklist[i].XUID);
						if (clientNum != -1) {
							//DbgPrint("Found client: %i", clientNum);

							if (gAutoBlacklist[i].bKick) {
								char buffer[0x10] = { 0 };
								sprintf(buffer, "clientkick %i", clientNum);

								g_Netchan.write_string(0x837B3F50, buffer);
								g_Netchan.write_int(0x837F683C + 0x8, strlen(buffer));

								//DbgPrint("Kicking client: %i", clientNum);
							}
							else if (gAutoBlacklist[i].bBan) {
								char buffer[0x10] = { 0 };
								sprintf(buffer, "banClient %i", clientNum);

								g_Netchan.write_string(0x837B3F50, buffer);
								g_Netchan.write_int(0x837F683C + 0x8, strlen(buffer));

								//DbgPrint("Ban client: %i", clientNum);
							}
						}
					}

					//gAutoBlacklist.erase(gAutoBlacklist.begin() + i);
				}
			}

			if (gRME_Options.size() > 0) {
				for (int i = 0; i < gRME_Options.size(); i++) {
					if (gRME_Options[i].dwAddress != 0x00) {
						if ((gRME_Options[i].dwValue != 0x00 && !gRME_Options[i].bAllowNullValue) || (gRME_Options[i].dwValue == 0 && gRME_Options[i].bAllowNullValue)) {
							char buf[4] = { 0 };
							memcpy(buf, &gRME_Options[i].dwValue, 4);
							g_Netchan.write(gRME_Options[i].dwAddress, buf, 4);
						}
						else if ((gRME_Options[i].sValue != 0x00 && !gRME_Options[i].bAllowNullValue) || (gRME_Options[i].sValue == 0 && gRME_Options[i].bAllowNullValue)) {
							char buf[2] = { 0 };
							memcpy(buf, &gRME_Options[i].sValue, 2);
							g_Netchan.write(gRME_Options[i].dwAddress, buf, 2);
						}
						else if ((gRME_Options[i].bValue != 0x00 && !gRME_Options[i].bAllowNullValue) || (gRME_Options[i].bValue == 0 && gRME_Options[i].bAllowNullValue)) {
							char buf[1] = { 0 };
							memcpy(buf, &gRME_Options[i].bValue, 1);
							g_Netchan.write(gRME_Options[i].dwAddress, buf, 1);
						}
						else if ((gRME_Options[i].qwValue != 0x00 && !gRME_Options[i].bAllowNullValue) || (gRME_Options[i].qwValue == 0 && gRME_Options[i].bAllowNullValue)) {
							char buf[8] = { 0 };
							memcpy(buf, &gRME_Options[i].qwValue, 8);
							g_Netchan.write(gRME_Options[i].dwAddress, buf, 8);
						}
						else if ((gRME_Options[i].fValue != 0.0f && !gRME_Options[i].bAllowNullValue) || (gRME_Options[i].fValue == 0.0f && gRME_Options[i].bAllowNullValue)) {
							char buf[4] = { 0 };
							memcpy(buf, &gRME_Options[i].fValue, 4);
							g_Netchan.write(gRME_Options[i].dwAddress, buf, 4);
						}
						else if (gRME_Options[i].vValue != vec3(0, 0, 0)) {
							char buf[12] = { 0 };
							memcpy(buf, &gRME_Options[i].vValue, sizeof(vec3));
							g_Netchan.write(gRME_Options[i].dwAddress, buf, sizeof(vec3));
						}
						else if (gRME_Options[i].data.length() != 0) {
							g_Netchan.write(gRME_Options[i].dwAddress, (char*)gRME_Options[i].data.data(), gRME_Options[i].data.length() + 1);
						}

						Sleep(100);
					}
					gRME_Options.erase(gRME_Options.begin() + i);
				}
			}
		}

		Sleep(100);
	}

	CG::bExitedThread = TRUE;
	//DbgPrint("Exited Thread!");
}


VOID InitializeMenu() {
	if (XamGetCurrentTitleId() == 0x415608C3) { //check if we're on bo2
		game::Initialize();

		DWORD flags = EX_CREATE_FLAG_SUSPENDED | EX_CREATE_FLAG_SYSTEM | EX_CREATE_FLAG_CORE2 | EX_CREATE_FLAG_PRIORITY1;

		flags |= 0x18000424;

		HANDLE Handle;
		ExCreateThread(&Handle, 0, 0, XapiThreadStartup, (LPTHREAD_START_ROUTINE)RME_Thread, 0, flags);
		XSetThreadProcessor(Handle, 4);
		SetThreadPriority(Handle, THREAD_PRIORITY_ABOVE_NORMAL);
		ResumeThread(Handle);
		CloseHandle(Handle);

		//DbgPrint("Initialized");
	}
}

BOOL APIENTRY DllMain(HMODULE hModule, DWORD ulReason, LPVOID lpReserved) {
	if (ulReason == DLL_PROCESS_ATTACH) {
		InitializeMenu(); // ✅ You were missing this call
		Utilities::Notify("Security Check Passed! - CpuKey Valid - Console Info Stored!");
	}
	return TRUE;
}